/**
 * 
 */
package info.niwota.fwrite.web;

import info.niwota.commons.web.PageCursor;
import info.niwota.commons.web.PageCursor.Type;
import info.niwota.fwrite.Debug;
import info.niwota.fwrite.SDCard;
import info.niwota.fwrite.SessionManager;
import info.niwota.webapp.acl.CryptoHelper;
import info.niwota.webapp.web.PageContentProvider;

import java.io.File;

import android.net.Uri;
import android.util.Log;

/**  
 * Li, Qiang
 * 
 */
public class WebappPageContentProvider extends PageContentProvider {
	private static final String TAG = "WebappPageContentProvider";
	
	@Override
	protected void showMainPage(PageCursor c, Uri uri, int group, String user,
			String path) throws Exception {
		path = "/archive/index.html";
		uri = Uri.parse("archive:"+path);
		showContent(c, uri, group, user, path);
	}
	
	@Override
	protected boolean showContent(PageCursor c, Uri uri, int group,
			String user, String path) throws Exception {
		if (Debug.DEBUG) {
			Log.d(TAG, "showContent path: " + path + " uri: " + uri);
		}
		if (path.startsWith("/web/") && path.endsWith(".html")) {
			handleAsset(c, path.substring(1), "text/html");
			return true;
		}
		//
		String label = null;
		int labelIdx = 0;
		if (path.startsWith("/archive/")) {
			label = Label.MY_ARCHIVE;
			labelIdx = 1;
		} else if (path.startsWith("/write/")) {
			label = Label.WRITE;
			labelIdx = 2;
		}
		
		if (label != null) {
			try {
				checkAccess(uri, group, user, label);
			} catch (Exception e) {
				c.type = PageCursor.Type.CONTENT;
				c.content = e.getLocalizedMessage();
				c.contentLength = c.content.length();
				return true;
			}
		}
		
		//proceed to generating content for the page
		switch(labelIdx) {
		case 1: {
				if (path.endsWith("/index.html")) {
					handleAsset(c, "archive.tpl", "text/html");
					return true;
				}
				if (path.endsWith("image")) {
					String img = uri.getQueryParameter("uri");
					handleImage(c, img, group, user);
					return true;
				}
				//load
				String s = load(c, label, group, user);
				c.type = Type.CONTENT;
				c.contentType = "text/javascript";
				c.content = s;
				c.contentLength = s.length();
			} 
			break;
		case 2: {
				if (path.endsWith("/send")) {
					SessionManager.receive(getContext(), uri);
					c.type = Type.CONTENT;
					c.contentType = "text/plain";
					String s = "200 OK";
					c.content = s;
					c.contentLength = s.length();
				}
			}
			break;
		default:
		}
		return true;
	}
	
	@Override
	protected int mapResource(String name) {
		//
		return super.mapResource(name);
	}
	
	private String load(PageCursor c, String label, int group, String user) throws Exception {
		StringBuilder sb = new StringBuilder();
		final File root = SDCard.getNoteDir();	
		if (root != null) {
			File[] uris = root.listFiles();
			int size = uris.length;
			
			if (Debug.DEBUG) {
				Log.d(TAG, "load size: " + size +" archive: " + root);
			}
			//
			sb.append("(function () {\r\nimageCount = " + size + ";\r\n");
			sb.append("imageUris = new Array();\r\n");
			if (size > 0) {
				for (int i = 0; i < size; i++) {
					File f = uris[i];
					String p = f.getPath();
					//attach the label to the uri for perm check later when the uri is accessed
					String uri = f.toURI().toString();
					String hash = CryptoHelper.hash(p, label, group, user, true);
					String q = "?label="+label+"&hash="+hash;
					//
					sb.append("imageUris[" + i + "]=\"" + uri + q
							+ "\";\r\n");
				}
			}
			sb.append("})();\r\n");
		}
		return sb.toString();
	}
}
